package com.wanboedu.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wanboedu.dto.InvoiceDTO;
import com.wanboedu.mapper.InvoiceMapper;
import com.wanboedu.result.PageResult;
import com.wanboedu.service.InvoiceService;
import com.wanboedu.vo.DataViewTwoVO;
import com.wanboedu.vo.InvoiceVO;
import com.wanboedu.vo.SellerDataVO;
import com.wanboedu.vo.TypeDataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class InvoiceServiceImpl implements InvoiceService {

    @Autowired
    private InvoiceMapper invoiceMapper;

    @Override
    public void submit(InvoiceDTO invoiceDTO) {
        invoiceDTO.setCreateTime(LocalDate.now());
        invoiceDTO.setUpdateTime(LocalDate.now());
        invoiceMapper.submit(invoiceDTO);

    }

    @Override
    public PageResult getList(InvoiceDTO invoiceDTO) {
        PageHelper.startPage(invoiceDTO.getPage(), invoiceDTO.getPageSize());
        Page<InvoiceDTO> invoiceDTOPage = invoiceMapper.getList(invoiceDTO);

        return new PageResult(invoiceDTOPage.getTotal(), invoiceDTOPage.getResult());
    }

    @Override
    public void delete(Integer id) {
        invoiceMapper.delete(id);
    }

    @Override
    public List<InvoiceVO> getAllList() {
        List<InvoiceVO> invoiceVOS = invoiceMapper.getAllList();
        return invoiceVOS;
    }

    @Override
    public List<DataViewTwoVO> getdateValue() {
        List<InvoiceVO> invoiceVOS = invoiceMapper.getdateValue();
        System.out.println(invoiceVOS.toString());
        List<DataViewTwoVO> dataViewTwoVOS = new ArrayList<>();
        invoiceVOS.forEach(invoiceVO -> {
            DataViewTwoVO dataViewTwoVO = new DataViewTwoVO();
            Double value = invoiceMapper.getAllvalue(invoiceVO.getCreateTime());
            System.out.println(value);
            String dateStr = invoiceVO.getCreateTime().substring(0, 10); // 截取前10个字符，即日期部分
            LocalDate date = LocalDate.parse(dateStr); // 现在可以安全地解析为LocalDate
            System.out.println(date);
            dataViewTwoVO.setDate(date);
            dataViewTwoVO.setValue(value);
            System.out.println(dataViewTwoVO);
            dataViewTwoVOS.add(dataViewTwoVO);
        });
        dataViewTwoVOS.sort(Comparator.comparing(DataViewTwoVO::getDate));
        return dataViewTwoVOS;
    }

    @Override
    public List<TypeDataVO> getTypeList() {
        List<InvoiceVO> invoiceVOS = invoiceMapper.getdateValue();
        List<TypeDataVO> typeDataVOS = new ArrayList<>();

        // 遍历invoiceVOS
        for (InvoiceVO invoiceVO : invoiceVOS) {
            String type = invoiceVO.getInvoiceType();
            Integer count = invoiceMapper.getTypeCount(type);

            // 查找typeDataVOS中是否已存在相同的letter
            boolean found = false;
            for (TypeDataVO typeDataVO : typeDataVOS) {
                if (typeDataVO.getLetter().equals(type)) {
                    // 如果找到，则更新frequency
                    found = true;
                    break;
                }
            }

            // 如果没有找到，则创建一个新的TypeDataVO并添加到列表中
            if (!found) {
                TypeDataVO newTypeDataVO = new TypeDataVO();
                newTypeDataVO.setLetter(type);
                newTypeDataVO.setFrequency(count);
                typeDataVOS.add(newTypeDataVO);
            }
        }

        // 返回最终的typeDataVOS列表
        return typeDataVOS;
    }

    @Override
    public List<SellerDataVO> getSellerList() {
        List<InvoiceVO> invoiceVOS = invoiceMapper.getdateValue();
        List<SellerDataVO> sellerList = new ArrayList<>();

        // 遍历invoiceVOS
        for (InvoiceVO invoiceVO : invoiceVOS) {
            String sellerName = invoiceVO.getSellerName();
            System.out.println(sellerName);
            Double price = invoiceMapper.getSellerCount(sellerName);
            System.out.println(price);
            // 查找typeDataVOS中是否已存在相同的letter
            boolean found = false;
            for (SellerDataVO Seller : sellerList) {
                if (Seller.getLetter().equals(sellerName)) {
                    Seller.setFrequency(price);
                    found = true;
                    break;
                }
            }
            if (!found) {
                SellerDataVO SellerDataVO = new SellerDataVO();
                SellerDataVO.setLetter(sellerName);
                SellerDataVO.setFrequency(price);
                sellerList.add(SellerDataVO);
            }
        }

        // 返回最终的typeDataVOS列表
        return sellerList;
    }
}
